package repo

import android.util.Log
import androidx.lifecycle.liveData
import com.kongzue.dialogx.DialogX
import com.kongzue.dialogx.dialogs.MessageDialog
import com.kongzue.dialogx.dialogs.PopTip
import utils.Constants

open class BaseRepository {
    suspend fun<T> dealResp(
        block:suspend() ->BaseResp<T>,
        liveData: RespStateData<T>)
    {
        var result = BaseResp<T>()
        result.responseState = BaseResp.ResponseState.REQUEST_START
        liveData.value = result
        result = block.invoke()
        Log.d("BaseRepository", result.errorCode.toString() + "/" + result.errorMsg)
        when (result.errorCode) {
            Constants.HTTP_SUCCESS -> {
                result.responseState = BaseResp.ResponseState.REQUEST_SUCCESS
            }
            Constants.HTTP_AUTH_INVALID -> {
                result.responseState = BaseResp.ResponseState.REQUEST_FAILED
                PopTip.show("请先登录")
            }
            else -> {
                result.responseState = BaseResp.ResponseState.REQUEST_FAILED
                PopTip.show("code:" + result.errorCode.toString() + " / msg:" + result.errorMsg)
            }
        }
    }
}